from abc import ABC

from tornado.web import RequestHandler
from common.mySQL import mySQL


# 作废############################
class ORM(RequestHandler, ABC):
    def save(self):
        # insert into students values('daf',21)
        tableName = self.__class__.__name__.lower()  # 当前调用对象的类名，即表名
        fieldsstr = "("
        valuesStr = "("
        for field in self.__dict__:  # table内容字典结构
            fieldsStr += (field + ",")
            if isinstance(self.__dict__[field], str):
                valuesStr += ("" + self._dict__[field] + "', ")
            else:
                valuesStr += (str(self.__dict__[field]) + ",")
            fieldsStr = fieldsstr[:len(fieldsstr) - 1] + ")"
            valuesStr = valuesStr[:len(valuesStr) - 1] + ")"
            sql = "INSERT INTO " + tableName + "VALUES" + valuesStr + ";"
            db = mySQL()
            db.insert(sql)

    def delete(self):
        pass

    def update(self):
        pass

    @classmethod
    def all(cls):
        tableName = cls.__name__.lower()
        sql = "SELECT * FROM " + tableName + ";"
        db = mySQL()
        return db.get_all_obj(sql, tableName)

    @classmethod
    def filter(cls):
        print('filter')
